﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.MarketData
{
    public static class MarketDataConstants
    {
        public const string GBp = "GBp";
        public const string GBX = "GBX";
        public const string GBP = "GBP";

        public enum Currencies
        {
            GBX,
            GBP,
            EUR,
            USD,
            SEK,
            DKK,
            HKD,
            AUD,
            SGD,
            NOK,
            CHF,
            ISK,
            CAD,
            RUB,
            NZD,
            JPY,
            XXX
        }


        public static bool TryGetMarketMICbyRIC(string ric, out MarketMICS market)
        {
            // if the entire RIC has be given then extract everything after the last .
            int index = ric.LastIndexOf('.');

            if (index >= 0)
            {
                ric = ric.Substring(index + 1);
            }            

            switch(ric)
            {
                case "L":
                    market = MarketMICS.XLON;
                    return true;
                case "BR":
                    market = MarketMICS.XBRU;
                    return true;
                case "OL":
                    market = MarketMICS.XOSL;
                    return true;
                case "ST":
                    market = MarketMICS.XSTO;
                    return true;
                case "VI":
                    //market = MarketMICS.XWBO;
                    market = MarketMICS.WBAH;
                    return true;
                case "DE":
                    market = MarketMICS.XETR;
                    return true;
                case "CO":
                    market = MarketMICS.XCSE;
                    return true;
                case "MC":
                    market = MarketMICS.XMCE;
                    return true;
                case "LS":
                    market = MarketMICS.XLIS;
                    return true;
                case "HE":
                    market = MarketMICS.XHEL;
                    return true;
                case "PA":
                    market = MarketMICS.XPAR;
                    return true;
                case "AS":
                    market = MarketMICS.XAMS;
                    return true;
                case "AT":
                    market = MarketMICS.XATH;
                    return true;
                case "I":
                    market = MarketMICS.XDUB;
                    return true;
                case "MI":
                    market = MarketMICS.XMIL;
                    return true;
                case "VX":
                case "S":
                    market = MarketMICS.XSWX;
                    return true;
                default:
                    market = MarketMICS.XXXX;
                    return false;
            }
        }


        public static bool TryParseCurrency(string currencyIn, out Currencies currencyOut)
        {
            switch (currencyIn)
            {
                case "GBP":
                    currencyOut = Currencies.GBP;
                    return true;
                case "USD":
                    currencyOut = Currencies.USD;
                    return true;
                case "GBX":
                    currencyOut = Currencies.GBX;
                    return true;
                case "EUR":
                    currencyOut = Currencies.EUR;
                    return true;
                case "DKK":
                    currencyOut = Currencies.DKK;
                    return true;
                case "NOK":
                    currencyOut = Currencies.NOK;
                    return true;
                case "SEK":
                    currencyOut = Currencies.SEK;
                    return true;
                case "CHF":
                    currencyOut = Currencies.CHF;
                    return true;
                default:
                    currencyOut = Currencies.XXX;
                    return false;
            }
        }


        public enum MarketCapSizes
        {
            Small,
            Medium,
            Large
        }

        /// <summary>
        /// Markets of listing
        /// </summary>
        public enum MarketMICS
        {
            /// <summary>
            /// No valid market
            /// </summary>
            XXXX,
            /// <summary>
            /// London
            /// </summary>
            XLON,
            /// <summary>
            /// Paris
            /// </summary>
            XPAR,
            /// <summary>
            /// Amsterdam
            /// </summary>
            XAMS,
            /// <summary>
            /// Luxumberg
            /// </summary>
            XLUX,
            /// <summary>
            /// Milan (MTA)
            /// </summary>
            XMIL,
            /// <summary>
            /// Frankfurt
            /// </summary>
            XETR,
            /// <summary>
            /// Copenhagen
            /// </summary>
            XCSE,
            /// <summary>
            /// Brussles
            /// </summary>
            XBRU,
            /// <summary>
            /// Lisbon
            /// </summary>
            XLIS,
            /// <summary>
            /// Helsinki
            /// </summary>
            XHEL,
            /// <summary>
            /// Oslo
            /// </summary>
            XOSL,
            /// <summary>
            /// Madrid
            /// </summary>
            XMCE,
            /// <summary>
            /// Stockholm
            /// </summary>
            XSTO,
            
            /// <summary>
            /// WIENER BOERSE - Vienna Broerse
            /// </summary>
            WBAH,
            
            /// <summary>
            /// Swiss
            /// </summary>
            XSWX,
            /// <summary>
            /// Warsaw Boerse
            /// </summary>
            XWAR,
            /// <summary>
            /// Iceland
            /// </summary>
            XICE,
            /// <summary>
            /// Irish
            /// </summary>
            XDUB,
            /// <summary>
            /// Athens
            /// </summary>
            XATH,
            /// <summary>
            /// NYSE Arca
            /// </summary>
            ARCX,
            /// <summary>
            /// International Securities Exchange
            /// </summary>
            XISX,
            /// <summary>
            /// NYSE Euronext Liffe 
            /// </summary>
            XLIF,
            /// <summary>
            /// Nasdaq
            /// </summary>
            XNAS,
            /// <summary>
            /// New York Stock Exchange
            /// </summary>
            XNYS,
            /// <summary>
            /// Toronto Stock Exchange
            /// </summary>
            XTSE,
            /// <summary>
            /// TSX Venture Exchange
            /// </summary>
            XTSX
        }

        /// <summary>
        /// Trading Venues or MTFs
        /// </summary>
        public enum ExchangeMICS
        {
            /// <summary>
            /// No valid exchange
            /// </summary>
            XXXX,
            /// <summary>
            /// Chi-X Europe
            /// </summary>
            CHIX,
            /// <summary>
            /// Turquoise
            /// </summary>
            TRQX,
            /// <summary>
            /// Bats Europe
            /// </summary>
            BATE,
            /// <summary>
            /// London Stock Exchange (as venue)
            /// </summary>
            XLON,
            /// <summary>
            /// Euronext Amsterdam (as venue)
            /// </summary>
            XAMS,
            /// <summary>
            /// Euronext Paris (as venue)
            /// </summary>
            XPAR,
            /// <summary>
            /// Euronext Luxumberg
            /// </summary>
            XLUX,
            /// <summary>
            /// Euronext Brussles
            /// </summary>
            XBRU,
            /// <summary>
            /// Milan (MTA)
            /// </summary>
            XMIL,
            /// <summary>
            /// Amex Options
            /// </summary>
            AMXO,
            /// <summary>
            /// NYSE Arca
            /// </summary>
            ARCX,
            /// <summary>
            /// Bats Trading US
            /// </summary>
            BATS,
            /// <summary>
            /// Chi-X Canada
            /// </summary>
            CHIC,
            /// <summary>
            /// DirectEdgeA
            /// </summary>
            EDGA,
            /// <summary>
            /// DirectEdgeX
            /// </summary>
            EDGX,
            /// <summary>
            /// Credit Suisse Algo Destination
            /// </summary>
            FBCO,
            /// <summary>
            /// Borsa Italiana
            /// </summary>
            MTAA,
            /// <summary>
            /// Nasdaq OMX Europe
            /// </summary>
            NOME,
            /// <summary>
            /// Nasdaq OMX Boston Options
            /// </summary>
            XBOS,
            /// <summary>
            /// Chicago Board Options Exchange
            /// </summary>
            XCBO,
            /// <summary>
            /// International Securities Exchange
            /// </summary>
            XISX,
            /// <summary>
            /// NYSE Euronext Liffe 
            /// </summary>
            XLIF,
            /// <summary>
            /// Nasdaq
            /// </summary>
            XNAS,
            /// <summary>
            /// New York Stock Exchange
            /// </summary>
            XNYS,
            /// <summary>
            /// Nasdaq Options Market
            /// </summary>
            XNDQ,
            /// <summary>
            /// Nasdaq OMX Philadelphia Options
            /// </summary>
            XPHL,
            /// <summary>
            /// NYSE Pacific Exchange
            /// </summary>
            XPSE,
            /// <summary>
            /// Toronto Stock Exchange
            /// </summary>
            XTSE,
            /// <summary>
            /// TSX Venture Exchange
            /// </summary>
            XTSX
        }


        public enum Side
        {
            Bid,
            Ask
        }

        public enum SecurityTypes
        {
            Stock = 1,
            Bond = 2,
            Currency = 3,
            Rate = 4,
            Volatility = 5,
            Index = 6,
            Future = 7,
            Option = 8,
            Commodity = 9,
            Compound = 10
        }


        public static bool TryGetMarketMICfromUMTF(string umtf, out MarketMICS marketMIC)
        {
            switch (umtf[umtf.Length - 1])
            {
                case 'l':
                    marketMIC = MarketMICS.XLON;
                    return true;
                case 'a':
                    marketMIC = MarketMICS.XAMS;
                    return true;
                case 'p':
                    marketMIC = MarketMICS.XPAR;
                    return true;
                case 'm':
                    marketMIC = MarketMICS.XMIL;
                    return true;
                case 'z':
                    marketMIC = MarketMICS.XSWX;
                    return true;
                case 'b':
                    marketMIC = MarketMICS.XBRU;
                    return true;
                case 'x':
                    marketMIC = MarketMICS.XLUX;
                    return true;
                case 'u':
                    marketMIC = MarketMICS.XLIS;
                    return true;
                case 'r':
                    marketMIC = MarketMICS.XICE;
                    return true;
                case 's':
                    marketMIC = MarketMICS.XSTO;
                    return true;
                case 'o':
                    marketMIC = MarketMICS.XOSL;
                    return true;
                case 'c':
                    marketMIC = MarketMICS.XCSE;
                    return true;
                case 'e':
                    marketMIC = MarketMICS.XMCE;
                    return true;
                case 'h':
                    marketMIC = MarketMICS.XHEL;
                    return true;
                case 'd':
                    marketMIC = MarketMICS.XETR;
                    return true;
                case 'v':
                    //marketMIC = MarketMICS.XWBO;
                    marketMIC = MarketMICS.WBAH;
                    return true;
                case 'i':
                    marketMIC = MarketMICS.XDUB;
                    return true;
                default:
                    marketMIC = MarketMICS.XXXX;
                    return false;
            }
        }


        public static bool TryGetUMTFSuffixFromMarketMIC(MarketMICS marketMIC, out char suffix)
        {
            switch (marketMIC)
            {
                case MarketMICS.XLON:
                    suffix = 'l';
                    return true;
                case MarketMICS.XAMS:
                    suffix = 'a';
                    return true;
                case MarketMICS.XPAR:
                    suffix = 'p';
                    return true;
                case MarketMICS.XMIL:
                    suffix = 'm';
                    return true;
                case MarketMICS.XSWX:
                    suffix = 'z';
                    return true;
                case MarketMICS.XBRU:
                    suffix = 'b';
                    return true;
                case MarketMICS.XLUX:
                    suffix = 'x';
                    return true;
                case MarketMICS.XLIS:
                    suffix = 'u';
                    return true;
                case MarketMICS.XICE:
                    suffix = 'r';
                    return true;
                case MarketMICS.XSTO:
                    suffix = 's';
                    return true;
                case MarketMICS.XOSL:
                    suffix = 'o';
                    return true;
                case MarketMICS.XCSE:
                    suffix = 'c';
                    return true;
                case MarketMICS.XMCE:
                    suffix = 'e';
                    return true;
                case MarketMICS.XHEL:
                    suffix = 'h';
                    return true;
                case MarketMICS.XETR:
                    suffix = 'd';
                    return true;
                case MarketMICS.WBAH:                    
                    suffix = 'v';
                    return true;
                case MarketMICS.XDUB:
                    suffix = 'i';
                    return true;
                default:
                    suffix = ' ';
                    return false;
            }
        }

    }
}
